﻿using Newtonsoft.Json;

namespace Aliphant.Tool.ExcelToJson.Helpers
{
    internal class JsonHelper
    {
        public static T? Load<T>(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return default(T);
            }

            string content = File.ReadAllText(filePath);
            return Deserialize<T>(content);
        }

        public static void Save(object obj, string filePath, bool indented = false)
        {
            string content = Serialize(obj, indented);
            File.WriteAllText(filePath, content);
        }

        public static string Serialize(object obj, bool indented = false)
        {
            Formatting formatting = indented ? Formatting.Indented : Formatting.None;
            return JsonConvert.SerializeObject(obj, formatting);
        }

        public static T? Deserialize<T>(string content)
        {
            return JsonConvert.DeserializeObject<T>(content);
        }
    }
}
